<template>
  <h1>企业注册</h1>
  <el-form ref="enterpriseForm" :model="enterprise" :rules="enterpriseRules" label-width="135px">
    <el-form-item label="名称" prop="name">
      <el-input v-model="enterprise.name" placeholder="请输入企业名称"></el-input>
    </el-form-item>

    <el-form-item label="统一社会信用代码" prop="unifiedSocialCreditCode">
      <el-input v-model="enterprise.unifiedSocialCreditCode" :maxlength="18"
                placeholder="请输入统一社会信用代码"></el-input>
    </el-form-item>

    <el-form-item label="密码" prop="password">
      <el-input v-model="enterprise.password" placeholder="请输入密码" type="password"></el-input>
    </el-form-item>
    <el-form-item label="再次输入密码" prop="password">
      <el-input v-model="enterprise.rePassword" placeholder="请再一次输入密码" type="password"></el-input>
    </el-form-item>

    <el-form-item label="电子邮件" prop="email">
      <el-input v-model="enterprise.email" placeholder="请输入企业邮箱"></el-input>
    </el-form-item>

    <el-form-item label="地址" prop="address">
      <el-input v-model="enterprise.address" placeholder="请输入企业地址"></el-input>
    </el-form-item>

    <el-form-item label="联系电话" prop="phone">
      <el-input v-model="enterprise.phone" placeholder="请输入联系电话"></el-input>
    </el-form-item>

    <el-form-item label="邮政编码" prop="EMSCode">
      <el-input v-model="enterprise.EMSCode" placeholder="请输入邮政编码"></el-input>
    </el-form-item>

    <el-form-item label="所属行业" prop="industryAffiliation">
      <el-input v-model="enterprise.industryAffiliation" placeholder="请输入所属行业"></el-input>
    </el-form-item>

    <el-form-item label="企业类型" prop="enterpriseType">
      <el-select v-model="enterprise.enterpriseType" placeholder="请选择企业类型">
        <el-option label="私企" value="民企"></el-option>
        <el-option label="国企" value="国企"></el-option>
        <!-- 添加更多企业类型选项 -->
      </el-select>
    </el-form-item>

    <el-form-item label="成立时间" prop="createdDate">
      <el-date-picker
          v-model="enterprise.createdDate"
          placeholder="选择成立日期"
          type="date">
      </el-date-picker>
    </el-form-item>

    <el-form-item label="企业联系人" prop="contactPerson">
      <el-input v-model="enterprise.contactPerson" placeholder="请输入企业联系人"></el-input>
    </el-form-item>

    <el-form-item label="企业详情" prop="description">
      <el-input v-model="enterprise.description" autosize placeholder="请输入企业详情" type="textarea"></el-input>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="register(enterprise)">注册</el-button>
      <el-button type="primary" @click="router.push({ name: 'login'})">去登陆</el-button>
      <el-button typeof="info" @click="switchStudentUserRegister(true)">切换为学生注册</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import {defineEmits, ref} from 'vue';
import {ElDatePicker, ElForm, ElFormItem, ElInput, ElMessage, ElSelect} from 'element-plus';
import type {EnterpriseRegister, ResponseInterface} from "@/model/type";
import {enterpriseRegisterAPI} from "@/api/UserAPI";
import router from "@/router";

const emit = defineEmits(['changeUserType']);

function switchStudentUserRegister(b: boolean) {
  emit('changeUserType', b);
}

const enterprise = ref<EnterpriseRegister>({
  EMSCode: "",
  account: "",
  address: "",
  contactPerson: "",
  createdDate: "",
  description: "",
  email: "",
  enterpriseType: "",
  id: "",
  industryAffiliation: "",
  name: "",
  password: "",
  phone: "",
  rePassword: "",
  unifiedSocialCreditCode: "",
  userType: ""
});

const enterpriseRules = {
  name: [{required: true, message: '请输入企业名称', trigger: 'blur'}],
  email: [
    {required: true, message: '请输入企业邮箱', trigger: 'blur'},
    {
      validator(_, value) {
        const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
        return emailRegex.test(value)
            ? Promise.resolve()
            : Promise.reject(new Error('请输入有效的电子邮件地址'));
      },
      trigger: ['blur', 'change'],
    },
  ],
  contactPerson: [{required: true, message: '请输入企业联系人', trigger: 'blur'}],
  password: [{required: true, message: '请输入密码', trigger: 'blur'}],
  rePassword: [
    {required: true, message: '确认密码不能为空', trigger: 'blur'},
    ({getFieldValue}) => ({
      validator(_, value) {
        if (!value || getFieldValue('password') === value) {
          return Promise.resolve();
        }
        return Promise.reject(new Error('两次输入的密码不一致'));
      },
      trigger: ['blur', 'change'],
    }),
  ],
  unifiedSocialCreditCode: [
    {
      required: true,
      message: '请输入统一社会信用代码',
      trigger: 'blur'
    },
    {
      validator(_, value) {
        if (value && value.length !== 18) {
          return Promise.reject(new Error('统一社会信用代码必须为18位'));
        }
        return Promise.resolve();
      },
      trigger: ['blur', 'change'],
    },
  ],
};

async function register(user: EnterpriseRegister) {
  user.userType = "enterprise"
  let r: ResponseInterface = await enterpriseRegisterAPI(user);
  if (r.code == 200) {
    ElMessage.success("注册成功")
    await router.push({path: '/login'})
  } else {
    ElMessage.error(r.msg)
  }
}

</script>
